//
//  MyScrollView.m
//  PhotoBrowserEx
//
//  Created by on 10-6-12.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "aMyScrollView.h"
#import "UIImageView+WebCache.h"
//#import "SVProgressHUD.h"

@interface aMyScrollView()


@end

@implementation aMyScrollView

#pragma mark -
#pragma mark === Intilization ===
#pragma mark -
- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
	{
		self.delegate = self;
		self.minimumZoomScale = 0.5;
		self.maximumZoomScale = 3.;
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
        
        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
        [singleTapGestureRecognizer setNumberOfTapsRequired:1];
        [self addGestureRecognizer:singleTapGestureRecognizer];
        
        UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
        [doubleTapGestureRecognizer setNumberOfTapsRequired:2];
        [self addGestureRecognizer:doubleTapGestureRecognizer];
        
        //这行很关键，意思是只有当没有检测到doubleTapGestureRecognizer 或者 检测doubleTapGestureRecognizer失败，singleTapGestureRecognizer才有效
        [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];

        imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.frame.size.width, WSCREENH_HEIGHT - 64)];

        imageView.userInteractionEnabled = YES;
        imageView.contentMode = UIViewContentModeScaleToFill;
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:imageView];

//        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WSCREENH_WIDTH, WSCREENH_HEIGHT)];
//        [webView loadHTMLString:_strContent baseURL:nil];
//        [self addSubview:webView];

    }
    return self;
}

- (void)setStrImgUrl:(NSString *)strImgUrl
{
    NSURL *imgUrl = [NSURL URLWithString:strImgUrl];
    
    [imageView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamed:@"diagram_placeholder.png"]];
//    [imageView sd_setImageWithURL:imgUrl placeholderImage:img options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
//
////        [SVProgressHUD show];
//
//    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
////        dispatch_main_async_safe(^{
////            [SVProgressHUD dismiss];
////        });
//    }];

}
#pragma mark -
#pragma mark === UIScrollView Delegate ===
#pragma mark -
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{	
	return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
	CGFloat zs = scrollView.zoomScale;
	zs = MAX(zs, 1.0);
	zs = MIN(zs, 2.0);
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3];		
	scrollView.zoomScale = zs;	
	[UIView commitAnimations];
}

- (void) doubleTap:(UITapGestureRecognizer *)tapGestureRecognizer{

    CGFloat zs = self.zoomScale;
    zs = (zs == 1.0) ? 2.0 : 1.0;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.zoomScale = zs;
    [UIView commitAnimations];

}

// 点击手势
- (void) singleTap:(UITapGestureRecognizer *)tapGestureRecognizer
{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"dismissMyScrollView" object:nil];
}


#pragma mark -
#pragma mark === dealloc ===
#pragma mark -
 
/*
- (void)dealloc
{
	[_image release];
	[imageView release];
	
    [super dealloc];
}
*/

@end
